package com.lance.answer.gui.answer.gk.video;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.lance.answer.dto.GkUser;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.DealInfoUtil;
import com.lance.answer.gui.answer.gk.video.dto.WatchVideoDto;
import com.lance.answer.gui.answer.gk.video.task.WatchVideoTask;
import com.lance.answer.question_export.gk_new.GetGkCourseInfoTask;
import com.lance.answer.util.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.FileInputStream;
import java.io.InputStream;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

@Slf4j
public class WatchVideo {

    public static final String JS = "var video = document.getElementsByTagName(\"video\");if(video.length>0){ video[0].src = \"https://jvod.300hu.com/vod/product/a7d50669-f57b-4da9-8cf8-43d04372cab0/0411e189a55b41cfada876041fa7fdd7.mp4?source=2&h265=h265/18799/d7d0ab77577f4ea0bcf5bc794316045a.mp4\";}";


    public static void main(String[] args)  throws Exception{

        int sync = 12;
        String driverPath = null;
//        //课程名
        String subjectName = "形势与政策";
        int subjectCode = 2660;
        int completeVideoSize = 5;

//                String subjectName = "毛泽东思想和中国特色社会主义理论体系概论";
//       // int subjectCode = 2660;
//        int completeVideoSize = 8;

//        String subjectName = "思想道德修养与法律基础";
//     //   String subjectName = "中国近现代史纲要";
//        int subjectCode = 4680;
//        int completeVideoSize = 8;

        List<String> userNames = DealInfoUtil.countUserCourseInfo("libs//userCourseInfo").get(subjectName);
        LinkedBlockingDeque<WatchVideoDto> watchVideoQueue = new LinkedBlockingDeque<>();


        InputStream inputStream2 =  new FileInputStream("libs//allUser.txt");
        List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

        Map<String, String> usernameAndPwd = Maps.newHashMap();
        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1].substring(6, 14);
            pwd = "Ouchn" + pwd;
            usernameAndPwd.put(userCode, pwd);
        }


        for(String username:userNames){
            String pwd = usernameAndPwd.get(username);
            WatchVideoDto videoDto = new WatchVideoDto();
            videoDto.setUserName(username);
            videoDto.setPassword(pwd);
            videoDto.setSubjectName(subjectName);
         //   videoDto.setSubjectCode(subjectCode);
            videoDto.setJS(JS);
            videoDto.setCompleteVideoSize(completeVideoSize);
            watchVideoQueue.add(videoDto);
        }


        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new WatchVideoTask(driverPath,watchVideoQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName( "答题线程--" + i);
            answerThread.start();
        }

    }







}

